<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp_lib/init.jsp"%>
<style type="text/css">
<!--
.x-tree-node-text, .x-accordion-hd {
	cursor: pointer;
}
-->
</style>
<script type="text/javascript">
	var treePanels = [];

	function openTab(title, code, url) {
		var tabId = "tab_" + code + Math.random();
		tabId = "tab_" + code;
		var panel = Ext.getCmp(tabId);
		if (!panel) {
			var href = "${path}" + url;
			var content = '<iframe scrolling="no" frameborder="0"  src="'
					+ href + '" style="width:100%;height:100%;"></iframe>';
			panel = new Ext.Panel({
				id : tabId,
				title : title,
				closable : true,
				bodyPadding : 0,
				html : content
			});
			Ext.getCmp('tabpanels').add(panel);
		}
		Ext.getCmp('tabpanels').setActiveTab(panel);
	}

	var leftPanel;
	Ext.onReady(function() {
		/** 
		 * 组建树 
		 */
		var buildTree = function(json) {
			return Ext.create('Ext.tree.Panel', {
				rootVisible : false,
				border : false,
				store : Ext.create('Ext.data.TreeStore', {
					root : {
						expanded : true,
						children : json.children
					}
				}),
				listeners : {
					'itemclick' : function(view, record, item, index, e) {
						if (record.isLeaf()) {
							var title = record.get("text");
							var code = record.get("code");
							var url = record.get("url");

							openTab(title, code, url);

						} else {
							//关闭节点
							//this.expandNode(record);
						} //#if
					},
					scope : this
				}
			});
		};

		/** 
		 * 左,panel.Panel 
		 */
		leftPanel = Ext.create('Ext.panel.Panel', {
			region : 'west',
			title : '导航栏',
			width : 230,
			layout : 'accordion',
			collapsible : true
		});

		/** 
		 * 加载菜单树 
		 */
		Ext.Ajax.request({
			url : '${path}/cp/frame/left_data.htm',
			success : function(response) {
				var json = Ext.JSON.decode(response.responseText)
				Ext.each(json, function(el) {
					var panel = Ext.create('Ext.panel.Panel', {
						id : el.id,
						title : el.text,
						layout : 'fit'
					});
					var treePanel = buildTree(el);
					treePanels.push(treePanel);
					panel.add(treePanel);
					leftPanel.add(panel);
				});
			},
			failure : function(request) {
				Ext.MessageBox.show({
					title : '操作提示',
					msg : "连接服务器失败",
					buttons : Ext.MessageBox.OK,
					icon : Ext.MessageBox.ERROR
				});
			},
			method : 'post'
		});

		//加载 Task 任务
		new Ext.util.TaskRunner().newTask({
			run : function() {
				loadTask();
			},
			interval : 300 * 1000
		}).start();
	});

	//加载任务
	function loadTask() {
		Ext.Ajax.request({
			url : '${path}/cp/task/my_task_data.htm',
			success : function(response) {
				var json = Ext.JSON.decode(response.responseText)
				Ext.each(json, function(el) {
					var code = el["code"];
					var cnt = el["cnt"];

					for (var i = 0; i < treePanels.length; i++) {
						var tp = treePanels[i];
						var node = tp.getStore().getNodeById(code);
						if (node != null) {
							var text = node.get("text");

							if (cnt == 0) {
								node.set("text", text);
							} else {
								node.set("text", text
										+ "<span style='color:red;'>(" + cnt
										+ ")</span>");
							}

							node.data.text = text;
						}
					}

				});
			},
			method : 'post'
		});
	}
</script>